#!/usr/bin/env python
#/**
# *  Copyright (C) 2005 PGT-Linux.org http://www.pgt-linux.org
# *  Author: vandorpe Olivier <vandorpeo@pgt-linux.org>
# *
# *  This program is free software; you can redistribute it and/or modify
# *  it under the terms of the GNU General Public License as published by
# *  the Free Software Foundation; either version 2 of the License, or
# *  (at your option) any later version.
# *
# *  This program is distributed in the hope that it will be useful,
# *  but WITHOUT ANY WARRANTY; without even the implied warranty of
# *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# *  GNU General Public License for more details.
# *
# *  You should have received a copy of the GNU General Public License
# *  along with this program; if not, write to the Free Software
# *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# */

import gtk
import gobject
import sys,socket


class testUSBB2K:
  
  s=None

  def KeyTimeOut(self):
    print "KeyTimeOut"

  def ButtonTextSender( self , widget , data ):
    print data
    self.s.send( data )
    
  def TextSender( self , widget , data ):
    if widget.get_active():
      print data ,
      self.s.send( data )
      end_iter = self.buffer.get_end_iter()
#      if not end_iter:
#        return False
      self.textview.scroll_to_iter(end_iter, 0, False, 1, 1)
      return False
      

  
  def usbb2k_interrupt(self,sock,mode):
    usbb2k_event=sock.recv(200)
    if not usbb2k_event:
      sys.exit("Error usbb2k sock\n")
    self.buffer.insert_at_cursor(usbb2k_event)
    return True

  def destroy(self,widget,data=None):
    gtk.main_quit()
  
  def __init__(self):
    self.ring_state=False
    self.s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
    self.s.connect("/tmp/usbb2k.sock")
    if self.s:
      th_usbb2k_read=gobject.io_add_watch(self.s,gobject.IO_IN,self.usbb2k_interrupt)
      self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
      self.window.set_title("USBB2K-API TEST")
      self.window.connect("destroy",self.destroy)
      self.window.set_border_width(0)
      self.box1=gtk.VBox(False,0)
      self.window.add(self.box1)
      
      sw=gtk.ScrolledWindow()
      sw.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
      self.textview=gtk.TextView(buffer=None)
      self.textview.set_editable(False)
      self.textview.set_size_request(gtk.gdk.screen_width()/2, gtk.gdk.screen_height()/5)
      self.buffer=self.textview.get_buffer()
      sw.add(self.textview)
      sw.show() 
      self.textview.show()
      self.box1.pack_start(sw)


      # PSTN / USB
      button=gtk.RadioButton(None ,"_PSTN")
      button.connect("toggled",self.TextSender,"SWITCH PSTN\n")
      self.box1.pack_start(button,True,True,0)
      button.show()
      
      button=gtk.RadioButton(button,"_USB")
      button.connect("toggled",self.TextSender,"SWITCH USB\n")
      self.box1.pack_start(button,True,True,0)
      button.show()


      # RING 
      
      button=gtk.RadioButton(None,"_Ring off")
      button.connect("toggled",self.TextSender,"RING 0\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      button=gtk.RadioButton(button,"Ri_ng tone1")
      button.connect("toggled",self.TextSender,"RING 1\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      button=gtk.RadioButton(button,"R_ing tone 2")
      button.connect("toggled",self.TextSender,"RING 2\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      #DIALTONE
      button=gtk.RadioButton(None,"DIALTONE _OFF")
      button.connect("toggled",self.TextSender,"DIALTONE OFF\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      
      button=gtk.RadioButton(button,"DIALTONE _ON")
      button.connect("toggled",self.TextSender,"DIALTONE ON\n")
      self.box1.pack_start(button,True,True,0)
      button.show()



      button=gtk.Button("_INFO")
      button.connect("clicked",self.ButtonTextSender,"INFO\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      #PICKUP / DROP PSTN
      button=gtk.Button("PICKUP__PSTN")
      button.connect("clicked",self.ButtonTextSender,"PICKUP_PSTN\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      button=gtk.Button("HANGUP__PSTN")
      button.connect("clicked",self.ButtonTextSender,"HANGUP_PSTN\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      
      button=gtk.Button("JOIN__USB__AND__PSTN (b3g)")
      button.connect("clicked",self.ButtonTextSender,"JOIN_USB_AND_PSTN\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      button=gtk.Button("DETACH__USB__AND__PSTN (b3g)")
      button.connect("clicked",self.ButtonTextSender,"DETACH_USB_AND_PSTN\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      
      button=gtk.Button("INVALID COMMAND")
      button.connect("clicked",self.ButtonTextSender,"INVALID COMMAND\n")
      self.box1.pack_start(button,True,True,0)
      button.show()

      
      button=gtk.Button("BIGTEST")
      button.connect("clicked",self.ButtonTextSender,"BIGTEST\n")
      self.box1.pack_start(button,True,True,0)
      button.show()


#       button=gtk.Button("URB-TEST 16")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 16\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("URB-HANGUP-PSTN-PART-1 17")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 17\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("URB-TEST3 18")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 18\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("URB-TEST4 19")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 19\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("URB-HANGUP-PSTN-PART-2 20")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 20\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("URB_TEST6 21")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 21\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("URB-TEST7 22")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 22\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("URB-PICKUP-PSTN-PART-1 23")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 23\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("URB_PICKUP_PSTN_PART_2 24")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 24\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("URB-TEST8 27")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 27\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

#       button=gtk.Button("BOTH_LEDS_ON 28")
#       button.connect("clicked",self.ButtonTextSender,"API_DEBUG 28\n")
#       self.box1.pack_start(button,True,True,0)
#       button.show()

      self.box1.show()
      self.window.show()
  def main(self):
      gtk.main()

if __name__=="__main__":
  usbb2k=testUSBB2K()
  usbb2k.main()

